package com.cl.edu.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cl.core.orm.Page;
import com.cl.core.orm.SearchFilter;
import com.cl.core.orm.hibernate.HibernateWebUtils;
import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.dao.BookingstudentDAO;
import com.cl.edu.dao.BookingteacherDAO;
import com.cl.edu.dao.SubjectDAO;
import com.cl.edu.dao.TeacherDAO;
import com.cl.edu.entity.Bookingstudent;
import com.cl.edu.entity.Bookingteacher;
import com.cl.edu.entity.Subject;
import com.cl.edu.entity.Teacher;
 
@Service
@Transactional
public class TeacherQueryManager {
	@Autowired
	private TeacherDAO teacherDAO;
	@Autowired
	private SubjectDAO subjectDAO;
	@Autowired
	private BookingteacherDAO bookingteacherDAO;
	@Autowired
	private BookingstudentDAO bookingstudentDAO;

	/**
	 * 通过id获取教师信息
	 * 
	 * @param id
	 * @return
	 */
	public Teacher getTeacherById(Long id) {
		return teacherDAO.get(id);
	}

	public Page<Bookingteacher> getBookingTeacherSuccess(
			Page<Bookingteacher> page, Long teaid) {
		StringBuffer hql = new StringBuffer(
				"from Bookingteacher bookingteacher");
		hql.append(" where bookingteacher.teacher=").append(teaid).append("");
		hql.append(" and bookingteacher.state=").append(1).append("");
		hql.append(" order by bookingteacher.id desc");
		page = bookingteacherDAO.findPage(page, hql.toString());
		return page;
	}

	public Page<Bookingstudent> getBookingStudentSuccess(
			Page<Bookingstudent> page, Long teaid) {
		StringBuffer hql = new StringBuffer(
				"from Bookingstudent bookingstudent");
		hql.append(" where bookingstudent.teacher=").append(teaid).append("");
		hql.append(" and bookingstudent.state=").append(1).append("");
		hql.append(" order by bookingstudent.id desc");
		page = bookingstudentDAO.findPage(page, hql.toString());
		return page;
	}

	public Page<Bookingstudent> getBookingStudent(Page<Bookingstudent> page,
			Long stuid) {
		StringBuffer hql = new StringBuffer(
				"from Bookingstudent bookingstudent");
		hql.append(" where bookingstudent.student=").append(stuid).append("");
		hql.append(" order by bookingstudent.teacher.step desc");
		page = bookingstudentDAO.findPage(page, hql.toString());
		return page;
	}

	public boolean checkBooked(Long teaid, Long stuid) {
		StringBuffer hql = new StringBuffer(
				"from Bookingstudent bookingstudent");
		hql.append(" where bookingstudent.student=").append(stuid).append("");
		hql.append(" and bookingstudent.teacher=").append(teaid).append("");
		Page<Bookingstudent> page = new Page<Bookingstudent>();
		page = bookingstudentDAO.findPage(page, hql.toString());
		return page.getResult().size() > 0;
	}

	/**
	 * 分页查询找家教信息
	 * 
	 * @param
	 * @return
	 */
	public Page<Teacher> getChoiceListByIds(Page<Teacher> page) {
		StringBuffer hql = new StringBuffer("from Teacher teacher");
		hql.append(" where teacher.state='").append("0'");
		hql.append(" and teacher.jianliFlag='").append("0'");
		List<SearchFilter> filters = HibernateWebUtils.buildSearchFilters(
				Struts2Utils.getRequest(), "filter_");
		for (SearchFilter filter : filters) {
			if ("fi".equals(filter.getPropertyName())) {
				String[] ids = filter.getPropertyValue().split(";");
				hql.append(" and (1<>1 or ");
				for (String id : ids) {
					String []temp=id.split("-");
					String tempHQL="from Subject sub where id>="+temp[0]+" and id<="+temp[1]+"";
					List<Subject> sub=subjectDAO.find(tempHQL);
					for(Subject s:sub){
						hql.append("(teacher.firstSubject like '%").append(s.getName()).append("%') or");
						hql.append("(teacher.otherSubject like '%").append(s.getName()).append("%') or");
					}
				}
				hql.append(" 2<>2)");
			}
		}
		hql.append(" order by teacher.").append(page.getOrderBy()).append(" ")
				.append(page.getOrder());
		page = teacherDAO.findPage(page, hql.toString());

		return page;
	}

	/**
	 * 分页查询找家教信息
	 * 
	 * @param
	 * @return
	 */
	public Page<Teacher> getChoiceList(Page<Teacher> page, Long statusType) {
		StringBuffer hql = new StringBuffer("from Teacher teacher");
		hql.append(" where 1=1 ");
		hql.append(" and teacher.state='").append("0'");
		hql.append(" and teacher.jianliFlag='").append("0'");
		List<SearchFilter> filters = HibernateWebUtils.buildSearchFilters(
				Struts2Utils.getRequest(), "filter_");
		if (statusType != 0) {
			hql.append(" and teacher.indentityFlag=").append(statusType)
					.append(" ");
		}
		for (SearchFilter filter : filters) {
			if ("subject".equals(filter.getPropertyName())
					&& !(filter.getPropertyValue()).equals("请选择家教科目")) {
				hql.append(" and ( ");
				String[] subs = filter.getPropertyValue().split(",");
				for (int i = 0; i < subs.length; i++) {
					if (i != 0) {
						hql.append(" or ");
					}
					hql.append("teacher.firstSubject like '%").append(subs[i])
							.append("%'");
				}
				hql.append(")");
			} else if ("sex".equals(filter.getPropertyName())
					&& !(filter.getPropertyValue()).equals("性别")) {
				hql.append(" and teacher.sex='").append(
						filter.getPropertyValue()).append("'");
			} else if ("areaid".equals(filter.getPropertyName())
					&& !(filter.getPropertyValue()).equals("授课区域")) {
				hql.append(" and teacher.teachPlace like '%").append(
						filter.getPropertyValue()).append("%'");
			} else if ("hx".equals(filter.getPropertyName())
					&& !(filter.getPropertyValue()).equals("请选择高校")) {
				hql.append(" and (teacher.studySchool='").append(
						filter.getPropertyValue()).append("'").append(
						" or teacher.teaSchool='").append(
						filter.getPropertyValue()).append("'").append(
						" or teacher.teaSchool2='").append(
						filter.getPropertyValue()).append("')");
				;
			} else if ("status".equals(filter.getPropertyName())
					&& !(filter.getPropertyValue()).equals("请选择身份信息")) {
				switch (statusType.intValue()) {
				case 0: {
					hql.append(" and (teacher.indentityStu='").append(
							filter.getPropertyValue()).append("'").append(
							" or teacher.indentityTea='").append(
							filter.getPropertyValue()).append("'").append(
							" or teacher.indentityOut='").append(
							filter.getPropertyValue()).append("')");
				}
					break;
				case 1: {
					hql.append(" and teacher.indentityStu='").append(
							filter.getPropertyValue()).append("'");
				}
					break;
				case 2: {
					hql.append(" and teacher.indentityTea='").append(
							filter.getPropertyValue()).append("'");
				}
					break;
				case 3: {
					hql.append(" and teacher.indentityOut='").append(
							filter.getPropertyValue()).append("'");
				}
					break;
				}
			} else if ("step".equals(filter.getPropertyName())) {
				if ("-1".equals(filter.getPropertyValue())) {
					hql.append(" and teacher.step<>'").append("0'");
				} else {
					hql.append(" and teacher.step='").append(
							filter.getPropertyValue()).append("'");
				}
			}
		}
		hql.append(" order by teacher.").append(page.getOrderBy()).append(" ")
				.append(page.getOrder());
		page = teacherDAO.findPage(page, hql.toString());

		return page;
	}

	/**
	 * 分页查询找星级会员家教信息
	 * 
	 * @param
	 * @return
	 */
	public Page<Teacher> getChoiceList(Page<Teacher> page) {
		StringBuffer hql = new StringBuffer("from Teacher teacher");
		hql.append(" where teacher.step<>'").append("0'");
		hql.append(" and teacher.state='").append("0'");
		hql.append(" and teacher.jianliFlag='").append("0'");
		hql.append(" order by teacher.").append(page.getOrderBy()).append(" ")
				.append(page.getOrder());
		page = teacherDAO.findPage(page, hql.toString());

		return page;
	}
	/**
	 * 查询一周之星
	 * 
	 * @param
	 * @return
	 */
	public Page<Teacher> getWeekStarList(Page<Teacher> page) {
		StringBuffer hql = new StringBuffer("from Teacher teacher");
		hql.append(" where teacher.state='").append("0'");
		hql.append(" and teacher.jianliFlag='").append("0'");
		hql.append(" and teacher.isWeekStar>").append("0");
		hql.append(" order by teacher.").append(page.getOrderBy()).append(" ")
				.append(page.getOrder());
		page.setPageSize(8);
		page = teacherDAO.findPage(page, hql.toString());
		return page;
	}
}
